Those who are new to 3D graphics might ask the
question, “Do I need to know math to create 3D graphics?” The simple
answer is “Yes, there is a level of mathematics that is required when
you are working on a 3D game.” Can you get by without knowing much?
Absolutely, and we have seen many examples where people have created a
3D game but don’t necessarily understand how they are getting the
results they see. This often leads to questions such as, “Why is my
model orbiting around the camera when I wanted it to rotate on its axis
like a planet?”
Without a doubt, having a
better understanding of underlying mathematics leads you to be less
confused, more quickly understand new concepts, and be more productive.
This section attempts to
strike a balance of theory and practical use. The goal is to give you
the higher level concepts that are used throughout 3D graphics without
having to explain the details of how to do specific mathematical
operations such as vector arithmetic or matrix multiplication. There
are countless educational sources on these operations, and they are
already implemented in the math types provided in XNA Game Studio. We
spend our time focusing on what these operations mean geometrically and
how they are used within 3D graphics.
Coordinate Systems
We already used one type of
coordinate system, which were screen coordinates when drawing sprites
on the screen. In screen coordinate space, there are two dimensions:
one for each of the X and Y directions. In screen coordinates, the X
direction increases in value from the left side of the screen to the
right, and the Y increases in value from the top of the screen to the
bottom.
There are two main types of
3D coordinate systems used in computer graphics. These two different
types differ in direction from the positive Z points relative to the X
and Y axes. The two types of coordinate systems are called right-handed
and left-handed (see Figure 1).
Both of these systems contain three directions for the X, Y, and Z
axes. The three axes converge at a central point called the origin
where their values equal 0. The values along each axis either gain or
lower in value at regular intervals depending on whether you are moving
in the positive or negative direction along that axis.
To visualize these two different
coordinate systems, let’s do a quick exercise. Take your right hand and
make a fist. Turn your hand so your bent fingers face you and the top
of your hand faces away from you. Now, extend your thumb. It should
point directly to your right. Your thumb represents the positive X
axis. Now extend your index finger, also known as your pointer finger,
directly up in the air. This represents the positive Y direction. Your
hand should look like it is making a capital L now with your thumb
facing right and representing the positive X axis and your index finger
pointing upwards representing the positive Y axis. Finally, extend your
middle finger and point it directly at yourself. Your middle finger
represents the positive Z axis (see Figure 2).
Imagine lines that extend
from the tips of all three of your fingers; they should be 90 degrees
apart from each other forming right angles between themselves. Having
each axis be at right angles from each other creates what is called an
orthogonal coordinate system. In this case, you created a right-handed
coordinate system. This is the coordinate system used by default in XNA
Game Studio. In a right-handed coordinate system, the negative Z axis
is used for the forward direction.
Note
DirectX by default uses
a left-handed coordinate system. The properties and methods in XNA Game
Studio assume you are using a right-handed coordinate system. Some
methods also provide a left-handed equivalent for those who want to use
a left-handed system.
To visualize a
left-handed system, follow the previous instruction except face your
palm away from yourself but still make a capital L with your thumb and
index finger for the positive X and Y axes. Now when you extend your
middle finger to form the positive Z axis, notice that it now points
away from you. In a left-handed coordinate system, the positive Z axis
is used for the forward direction (see Figure 3).
Note
Although we just stated
that Z axis is used for the forward direction, this is not always true.
In some 3D art creation packages, the Z axis actually represents the up
direction. The good news is that this difference is accounted for when
XNA Game Studio loads models from those specific 3D content packages.